package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        //每个人只能查看自己的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //先判断当前添加到购物车的商品是否已经存在
        //如果已经存在，则取出来原来的数据里面的number，加一然后设置回去
        //如果不存在则新增
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if(null != shoppingCartList && shoppingCartList.size() == 1){
            //证明已经存在，更新数量，在原来的基础上加一
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        }else{
            //证明不存在，新增
            // 判断当前添加的是菜品还是套餐。
            Long dishId = dto.getDishId();
            if(null != dishId){
                //当前加入购物车的必是菜品
                //Dish dish = dishMapper.getDishById(dishId); -> 改为MP
                Dish dish = dishMapper.getDishById(dishId);
//                Dish dish = dishMapper.selectById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                //添加到购物车的是套餐
                //改为MP TODO  Setmeal setmeal = setmealMapper.getSetmealById(Integer.parseInt(dto.getSetmealId()+""));
                Setmeal setmeal = setmealMapper.getSetmealById(Integer.parseInt(dto.getSetmealId()+""));
//                Setmeal setmeal = setmealMapper.selectById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        return shoppingCartMapper.list(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }

    @Override
    /**
     * 清空购物车商品
     */
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO dto) {

        //1.判断类型，

        //2.将当前购物车中的数量减一，并且判断如果number=0则调用删除方法

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        //每个人只能查看自己的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //先判断当前添加到购物车的商品是否已经存在
        //如果已经存在，则取出来原来的数据里面的number，加一然后设置回去
        //如果不存在则新增
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if(null != shoppingCartList && shoppingCartList.size() == 1){
            //证明已经存在，更新数量，在原来的基础上减一
            ShoppingCart cart = shoppingCartList.get(0);
            if(cart.getNumber() == 1){
                if(null != cart.getSetmealId()){
                    shoppingCartMapper.deleteBySetmealId(cart.getSetmealId());
                }else{
                    shoppingCartMapper.deleteByUserId(cart.getUserId());
                }
            }
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.updateNumberById(cart);
        }

    }


}
